package com.fjwt.gz.sync.bootstrap;

import cn.hutool.crypto.SmUtil;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import com.fjwt.gz.service.impl.SysConfigService;
import com.fjwt.gz.sync.config.SystemYmlConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/*
 *  项目初始化操作
 *  比如初始化配置文件， 读取基础数据， 资源初始化等。 避免在Main函数中写业务代码。
 *  CommandLineRunner  / ApplicationRunner都可以达到要求， 只是调用参数有所不同。
 *
*
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2021/6/8 17:04
*/
@Component
public class InitRunner implements CommandLineRunner {

    @Autowired private SystemYmlConfig systemYmlConfig;

    @Autowired private SysConfigService sysConfigService;

    @Override
    public void run(String... args) throws Exception {

        // 配置是否使用缓存模式
        SysConfigService.IS_USE_CACHE = systemYmlConfig.getCacheConfig();

        // 初始化系统秘钥
        SysConfigService.DB_ENCRYPT_SECRET = systemYmlConfig.getDbEncryptSecret();
        SysConfigService.DB_ENCRYPT_SM4 = SmUtil.sm4(SysConfigService.DB_ENCRYPT_SECRET.getBytes());
        SysConfigService.HTTP_MESSAGE_ENCRYPT_SECRET = systemYmlConfig.getHttpMessageEncryptSecret();
        SysConfigService.HTTP_MESSAGE_ENCRYPT_SM4 = SmUtil.sm4(SysConfigService.HTTP_MESSAGE_ENCRYPT_SECRET.getBytes());

        // 配置是否通信加密 和 密码修改
        SysConfigService.HTTP_MSG_IS_ENCRYPT = sysConfigService.getDBSecurityConfig().httpMsgIsEncrypt();
        SysConfigService.PWD_EXPIRED_MUST_RESET = sysConfigService.getDBSecurityConfig().passwordExpiredIsMustModify();


        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //fast-json 配置信息
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(config);
    }
}
